const fs = require('fs')
const regedit = require('regedit');
regedit.setExternalVBSLocation('./vbs');
const { join } = require("path");
import * as vdf from '@node-steam/vdf';
import Vue from 'vue'
console.log(vdf,'======1');
Vue.prototype.readFile = (func = null, model = 'json', path = "./config.ini") => {
    fs.readFile(path, 'utf-8', function (err, datas) {
        if (err) {
            Vue.prototype.writeFile({ userInfo: [] }, () => {
                func({ userInfo: [] })
            })

        } else {
            if (!datas) {
                Vue.prototype.writeFile({ userInfo: [] }, () => {
                    func({ userInfo: [] })
                })
            } else {
                if (func) {
                    switch (model) {
                        case 'json':
                            func(JSON.parse(datas))
                            return
                        case 'vdf':
                            func(vdf.parse(datas))
                            return
                        case 'default':
                            func(datas)
                            return
                    }

                }
            }
        }
    })
}
Vue.prototype.writeFile = (data, func = null, path = "./config.ini") => {
    fs.writeFile(path, JSON.stringify(data), function (err, datas) {
        if (err) {
            console.log(err);
        } else {
            if (func) { func(datas) }
        }
    });
}

Vue.prototype.getSteamRegedit = (path = "HKCU/SOFTWARE/Valve/Steam/Apps", func) => {
    regedit.list(join(path), function (err, result) {
        if (err) {
            console.log(err);
            return
        } else {
            func(result[join(path)])
        }
    })
}
function getSteamUrl() {
    Vue.prototype.getSteamRegedit('HKCU/SOFTWARE/Valve/Steam', (res) => {//获取注册项下的Steam路径
        if (res.values.SteamExe.value) {
            Vue.prototype.url = res.values.SteamExe.value
            readSteamConfig(res.values.SteamPath.value)
        } else {

            this.$store.commit("steamUrlState");
            console.log('未安装steam');
        }

    })
}

function readSteamConfig(SteamPath) {//读取安装的steam游戏
    Vue.prototype.readFile((datas) => {
        let library = []//处理好的steam库及游戏
        let libraryfolders = datas.libraryfolders//得到steam config libraryfolders的数据

        let i = 0
        for (const key in libraryfolders) {
            fs.readdir(libraryfolders[key].path + '\\steamapps', (err, files) => {//取到每个库下的游戏
                if (err) {
                    console.log(err);
                } else {
                    let acf = files.filter((res) => {
                        return res.includes(".acf")
                    })
                    library.push({//将得到的数据处理
                        path: libraryfolders[key].path + '\\steamapps',
                        acfs: acf
                    })

                }
                if (i === Object.keys(libraryfolders).length - 1) {//处理结束执行下一步
                    writeConfig(library)
                }
                i++

            })
        }


    }, "vdf", SteamPath + "\\config\\libraryfolders.vdf")
}
function writeConfig(data) {
    // let apps=[]//最终处理好的数据
    console.log(data);
    data.forEach((item, index) => {
        item['apps'] = []
        function call() {
            if (index === data.length - 1) {
                Vue.prototype.readFile(reData => {
                    reData['libraryfolders'] = data
                    Vue.prototype.writeFile({ ...reData });
                    console.log('------11-1');
                })
            }
        }
        item.acfs.forEach((items, i) => {
            Vue.prototype.readFile((datas) => {
                item.apps.push({
                    name: datas.AppState.name,
                    SizeOnDisk: datas.AppState.SizeOnDisk,
                    LastUpdated: datas.AppState.LastUpdated,
                    appid: datas.AppState.appid,
                    installdir: datas.AppState.installdir,
                })
                if (i === item.acfs.length - 1) {
                    call()
                }
            }, "vdf", item.path + '\\' + items)
        })
    })
}


export default {
    getSteamUrl
}

